from pyecharts.charts import *
from pyecharts import options as opts
import pandas as pd

import csv

data = []
df = pd.read_csv('../csv/new_works_info.csv')
df['author_id'] = df['author_id'].astype(str)
col = 'author_id'
df['length'] = df['author_id'].apply(len)
length_counts = df.groupby('length').size().reset_index(name='counts')
length_counts_list = list(length_counts.itertuples(index=False, name=None))


# print(length_counts)
# print(length_counts_list)

def get_pie_data(length_counts, i, data):
    data1 = (f'第{i}批作者数量', length_counts.iloc[i]["counts"])
    data.append(data1)
    return data1

def make_pie_3():
    for i in range(0, 6):
        get_pie_data(length_counts, i, data)
    chart = Pie(
    init_opts=opts.InitOpts(),
    )
    print(data)
    chart.add(
    '近期新老作者活动数量',
    data_pair=[('第0批作者数量', 1), ('第1批作者数量', 1), ('第2批作者数量', 6), ('第3批作者数量', 26), ('第4批作者数量', 77), ('第5批作者数量', 39)],
    )
    # chart.set_colors(['rgb(17, 0, 255)', 'rgb(255, 0, 0)', 'rgb(0,255,0)'])
    chart.set_global_opts(
    title_opts=opts.TitleOpts(
        title='近期新老作者活动数量',
        pos_left="center",
        pos_bottom="0",
        title_textstyle_opts=opts.TextStyleOpts(color="black"),
    ),
    legend_opts=opts.LegendOpts(is_show=False),
    )
    chart.set_series_opts(label_opts=opts.LabelOpts(formatter="{b}: {c}"))
    chart.render('../html/近期新老作者活动数量.html')
    return chart

if __name__ == "__main__":
    make_pie_3()
